//
//  ZZPersonVC.swift
//  QMXK
//
//  Created by jenkin on 2018/12/6.
//  Copyright © 2018年 jenkin. All rights reserved.
//

import UIKit

class ZZPersonVC: ZZBaseVC, UITableViewDelegate, UITableViewDataSource{
    var tableView:UITableView!
    var data:Array<AnyObject>!
    let personHeadID:String! = "personHeadID"

    override func viewDidLoad() {
        super.viewDidLoad()
        self.setUI()
        self.loadData()
    }
    
    override func setUI(){
        super.setUI()
        let tableH = CommenConst.KScreenH - self.bottomH
        self.tableView = UITableView.init(frame: CGRect.init(x: 0, y: 0, width: CommenConst.KScreenW, height: tableH), style: UITableView.Style.plain)
        self.tableView.rowHeight = 44
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.estimatedRowHeight = UITableView.automaticDimension
        self.tableView.register(ZZPersonHeadView.classForCoder(), forHeaderFooterViewReuseIdentifier: personHeadID)
        self.view.addSubview(self.tableView)
        self.tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: "cell")
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if self.data == nil{
         return 0
        }
        return self.data.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = self.data[indexPath.row]["name"] as? String
       // cell.backgroundColor = UIColor.orange
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView:ZZPersonHeadView = tableView.dequeueReusableHeaderFooterView(withIdentifier: personHeadID) as! ZZPersonHeadView
        headerView.scrollView = tableView
        return headerView
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return self.rate * 300.0 + self.navH
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        self.view.isUserInteractionEnabled = false
        let dict:Dictionary<String, Any>! = self.data![indexPath.row] as! Dictionary<String, Any>
        var data:Dictionary<String, Any> = [:]
        data["gp_no"] = dict["id"];
        weak var weakSelf = self
        ZZHttpRequest.shareRequest.gpRequest(target: GupiaoAPI.getGPDetail(data: data), successBlock: { (result:Dictionary) in
            weakSelf?.view.isUserInteractionEnabled = true
            var resultData = result
            let taskDetailVC:ZZTaskDetailVC = ZZTaskDetailVC()
            resultData["allMin"] = dict["min"]
            resultData["allMax"] = dict["max"]
            resultData["name"] = dict["name"]
            //将数据同步到本地
            ZZBaseUtil.shareUtil.saveTypeName(data: resultData)
            
            taskDetailVC.dataDict = resultData
            weakSelf?.navigationController?.pushViewController(taskDetailVC, animated: true)
        },failureBlock:
            {(data:Dictionary) in
                print("error")
                weakSelf?.view.isUserInteractionEnabled = true
        })
        
    }
    
    func loadData(){
        let data:Dictionary<String, Any> = [:]
        weak var weakSelf = self
        ZZHttpRequest.shareRequest.gpRequest(target: GupiaoAPI.getGP(data: data), successBlock: { (data:Dictionary) in
            weakSelf?.data = data["data"] as? Array<AnyObject>;
            weakSelf?.tableView .reloadData()
        },failureBlock:
            {(data:Dictionary) in
                print("error")
        })
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        loadData();
    }
    
}
